package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.preline.SearchPrelineBo;
import com.zhuss.bean.vo.product.preline.ListPrelineVo;
import com.zhuss.bean.vo.product.preline.ListPrelineVoEx;
import com.zhuss.bean.vo.product.prelinesite.ListPrelineSiteVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.PrelineDao;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import com.zhuss.product.db.dao.ext.PrelineSiteDao;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Preline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @description: 招募读
 * @author: mkh
 * @create: Mon Jan 04 13:38:02 CST 2021
 **/
@Component("prelineRead")
public class PrelineRead {
	
	@Autowired
	private PrelineDao prelineDao;

	@Autowired
	private PrelineSiteDao prelinesiteDao;

	@Autowired
	private SiteDao siteDao;

	@Autowired
	private PrelineSignDao prelineSignDao;

	public Map<String, Preline> listMap() {
		List<Preline> list = prelineDao.listAll();
		Map<String, Preline> map = new HashMap<>();
		for (Preline c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	/**
	 * 招募
	 * @return
	 */
	public Object listPreline() {
		List<Preline> prelineList = prelineDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(prelineList, result);
		return result;
	}
	
	
	/**
	 * 招募
	 * @return
	 */
	public Object listPreline(List<Long> prelineIdList) {
		List<Preline> result = prelineDao.listById(prelineIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询招募
	 * @param 
	 * @return
	 */
	public List<Preline> list(String name) {
		List<Preline> result = prelineDao.listByName(name);
		return result;
	}
	
	public List<Preline> listNotId(String name, Long prelineId) {
		List<Preline> result = new ArrayList<Preline>();
		List<Preline> list = list(name);
		for (Preline c : list) {
			if (!c.getPrelineId().equals(prelineId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 招募 
	 * @return
	 */
	public Object listPrelineId() {
		List<Preline> prelineList = prelineDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(prelineList, result);
		return result;
	}
	
	private void transferListId(List<Preline> prelineList, List<Long> result) {
		for (Preline item : prelineList) {
			result.add(item.getPrelineId());
		}
	}

	private void transferList(List<Preline> prelineList, List<ListVo> result) {
		for (Preline item : prelineList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getPrelineId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 招募列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchPrelineBo bo) {
		PageVo<ListPrelineVo> result = new PageVo<ListPrelineVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        List<ListPrelineVo> list  =prelineDao.list(bo);
		for (ListPrelineVo pre:list ) {
			pre.setApplyCount(prelineSignDao.countByPrelineId(pre.getPrelineId()));
		}
        result.setItems(list);
        result.setTotal(prelineDao.countList(bo));
		return result;
	}
	
	/**
	 * 招募详情
	 * @param prelineId
	 * @return
	 * @throws DzcxException
	 */
	public Object detail(Long prelineId) throws DzcxException {
		Object result = prelineDao.detail(prelineId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}

	/**
	 * 根据线路查询绑定的站点
	 * @param prelineId
	 * @return
	 * @throws DzcxException
	 */
	public Object bindSiteList(Long prelineId) throws DzcxException {

		List<ListPrelineSiteVo> res = prelinesiteDao.listByprelineId(prelineId);
		if (res == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		} else {
			res.forEach(siteVo -> {
				siteVo.setName(siteDao.selectByPrimaryKey(siteVo.getSiteId()).getName());
			});
		}

		return res;

	}

	public Object Prelinelist(SearchPrelineBo bo){
		PageVo<ListPrelineVoEx> result = new PageVo<ListPrelineVoEx>();
		result.setPageNum(bo.getPageNum());
		result.setPageSize(bo.getPageSize());
		List<ListPrelineVoEx> list  =prelineDao.prelinelistex(bo);
		for (ListPrelineVoEx pre:list ) {
			pre.setApplyCount(prelineSignDao.countByPrelineId(pre.getPrelineId()));
			bo.setPrelineId(pre.getPrelineId());
			int count = prelineSignDao.countApply(bo);
			if(count>0){
				pre.setIsApply(true);
			}else{
				pre.setIsApply(false);
			}
			List<ListPrelineSiteVo> res = prelinesiteDao.listByprelineId(pre.getPrelineId());
			for (ListPrelineSiteVo vo : res) {
				vo.setName(siteDao.selectByPrimaryKey(vo.getSiteId()).getName());
			}
			pre.setLinesiteList(res);
		}
		result.setItems(list);
		result.setTotal(prelineDao.countList(bo));
		return result;

	}
}
